﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using Mighty.Lib.Controls;
using Mighty.Lib.Document;

namespace Mighty
{
    public partial class MightyMainWindow
    {
        private void OpenButton_Click(object sender, EventArgs e)
        {
            mainWindowOpenFileDialog.Multiselect = true;
            DialogResult result = mainWindowOpenFileDialog.ShowDialog();
            if (result == DialogResult.Cancel) return;


            Dictionary<string, object> loadParams = new Dictionary<string, object>();

            string[] filePaths = mainWindowOpenFileDialog.FileNames;
            string[] safeNames = mainWindowOpenFileDialog.SafeFileNames;
            for (int ctr = 0; ctr < filePaths.Length; ctr++)
            {

                Dictionary<string, object> lp = new Dictionary<string, object>();
                lp.Add("OpenFilePath", (object)filePaths[ctr]);
                lp.Add("Stream",(object)new FileStream(filePaths[ctr],FileMode.Open,FileAccess.Read));
                lp.Add("FileName", (object)safeNames[ctr]);
                IDocument doc = _documentRepo.Open(lp);
                _docs.Add(doc);
                AddNewEditorWindow(doc);
            }
            GiveFocusToNewestTabPage(EditorTabControl.Controls.Count - 1);

            // last but not least... if we did an open on a New Document that we never
            // put any text into.. then let's just delete that sucker...
            if (((RichTextBox)this.EditorTabControl.Controls[0].Controls[0]).Text == "" &&
                !((DefaultDocumentTabPage)this.EditorTabControl.Controls[0]).Document.HasBeenSavedBefore) 
                this.EditorTabControl.Controls.RemoveAt(0);


        }
    }
}
